<?php

namespace StudyBuddy\Image;

/**
 * Abstract class for image parsing
 *
 */
abstract class Editor extends \StudyBuddy\StudyBuddyObject {

    /**
     * Type of original image
     * this will be one of these:
     * 'jpeg', 'gif' or 'png'
     *
     * @var string
     */
    protected $origType;

    /**
     * result of getimagesize()
     * call on original image
     * @var unknown_type
     */
    protected $aOrigSize;

    /**
     * Compression level
     * for saving jpeg images
     * default is 100, meaning no compression
     * (highest quality)
     *
     * @var int
     */
    protected $intCompression = 100;

    /**
     * A path based on ID
     *
     * @var string
     */
    protected $sHexPath;

    /**
     * resizing factor
     *
     * @var int
     */
    protected $factor = 1;

    /**
     * File extension of the thumb and work image
     * This is determined during the parsing
     * of the actual file.
     * This is not always the same as the extension
     * of the original file because image parser may automatically
     * change image extension, for example if image is in .bmp
     * format, it may be converted to .gif
     *
     * @var string
     */
    protected $ext;

    /**
     * Can hold array of image resources
     */
    protected $aResources = array();

    /**
     * Instantiate sub-class of this class
     * based on installed php modules (try Imagick, then GD)
     * Sets the hdlOrig if sImgPath is provided by calling
     * the loadImage() method
     *
     * @param object $oGlobal object
     *
     * @param string $sImgPath a path to image
     * which will be used to load original image
     *
     * @param bool $bUseGDis set to true,
     * then use GD functions even if magickwand or imagick
     * extensions are available
     *
     * @param object $oStatus object of type clsStatus
     * This is used to keep sending out status messages
     * to browser while
     * image is being processed
     *
     * @return object $this
     *
     * @throws StudyBuddyImageException if sub-class could
     * not be instantiated for any reason
     */
    public static final function factory(\StudyBuddy\Registry $oRegistry) {

        /**
         * @todo later may add
         * support for imagick
         */
        if (extension_loaded('gd')) {
            return new EditorGD($oRegistry);
        }

        throw new \StudyBuddy\Exception('Unable to parse images because neither GD nor Imagick extensions are available');
    }

    /**
     * Getter for $this->origType
     * @return string
     */
    public function getOrigType() {
        return $this->origType;
    }

    /**
     * Must create $this->hdlOrig resource
     * from the input image path
     * or throw exception if could not do this
     */
    abstract public function loadImage($sPath);

    abstract public function save($sPath, $hdlImg = null, $intCompression = null);

    abstract public function scale($intWidth, $intHeight);

    abstract public function makeSquare($intWidth);

    //abstract public function addAvatar(\StudyBuddy\User $User, $pathToImage);
    //abstract protected function detectBlank();

    abstract protected function addBorder($intBorderWidth = 1);

    /**
     * Must release orig
     * and work resources
     */
    abstract public function __destruct();
}
